
if exists("b:did_ftplugin")
	finish
endif
" vim9script
let b:did_ftplugin = 1

syntax on

function! s:matlab_fold_text(...) 
    let line = getline(v:foldstart)
    let sub = substitute(line, '%\s{{{\d\=', '', 'g')
    return v:folddashes . sub 
endfunction

function! s:matlab_fold_expr(...) 
	"值			代表 ~
	"0			这行不折叠
	"1, 2, ..		这行的折叠级别 1，2 等
	"-1			折叠级别没有定义，使用这行之前或之后一行的级别值，取其
	"中较小的一个。
	""="			使用上一行的折叠级别。
	""a1", "a2", ..	上一行的折叠级别加 1，2，..，结果适用于当前行
	""s1", "s2", ..	上一行的折叠级别减 1，2，..，结果适用于当前行
	""<1", "<2", ..	此折叠级别在本行结束
	"">1", ">2", ..	此折叠级别在本行开始

	if a:0 > 0
		let num = a:1
	else
		let num = v:lnum
	endif
	let line = getline(num)
	let idx = match(line, '% foldlevel: ')
	if idx >= 0
		" let sub = substitute(line[idx:], '% foldlevel: \(.\)', '&', '')
		" echo 'specify foldlevel to ' . sub
		return line[idx+12:]
	endif

	if match(line, '\<end\s%\s\(classdef\|properties\|methods\|function\|if\|for\|while\)') >= 0
		" echo "end"
		return 's1'
	endif
	if match(line, '\s%\s}}}') >= 0 
		return 's1'
	endif
	if match(line, '\s%\s{{{') >= 0 
		return 'a1'
	endif
	if match(line, '\<classdef\>') >= 0 
		" echo "classdef"
		return 1
	endif
	if match(line, '\<properties\>') >= 0
		" echo "properties"
		return 'a1'
	endif
	if match(line, '\<methods\>') >= 0
		" echo "methods"
		return 'a1'
	endif
	if match(line, '\<function\>') >= 0
		" echo "function"
		return 'a1'
	endif
	if match(line, '\<if\>') >= 0
		" echo "if"
		return 'a1'
	endif
	if match(line, '\<for\>\|\<while\>') >= 0
		" echo "for|while"
		return 'a1'
	endif
	if match(line, '^%% ') >= 0
		return 'a1'
	endif
	if match(line, '^\s*$') >= 0
		return '<1'
	endif
	return "="
endfunction

setlocal path=.,..,matlab,
setlocal tabstop=4
setlocal shiftwidth=4
setlocal expandtab
setlocal commentstring=%\ %s
setlocal textwidth=120
setlocal foldmethod=syntax
setlocal fileencoding=utf-8
setlocal suffixesadd=.m
setlocal cursorline
setlocal foldtext=<SID>matlab_fold_text()
setlocal foldexpr=<SID>matlab_fold_expr()

setlocal fdm=expr
let b:localleader = '\'

if !exists('g:matvim_auto_connect')
    packadd matvim
endif

let g:matlab_engine_list = get(g:, 'matlab_engine_list', []) 
if empty(g:matlab_engine_list) && exists('*MatlabFind')
    let g:matlab_engine_list = MatlabFind()
endif
if len(g:matlab_engine_list) > 0
    call MatlabConnect(g:matlab_engine_list[0])
endif


if exists('*MatlabRunCommand')
    command! -nargs=1 Matlab :call MatlabRunCommand(<f-args>)
    noremap <buffer> <Plug>(MatlabRunCommand)	 	:Matlab
    if !hasmapto('<Plug>(MatlabRunCommand)')
        map <buffer> <unique> <LocalLeader>m <Plug>(MatlabRunCommand)
    endif
endif

if exists('*MatlabRunFile')
    command! -nargs=0 MatlabRunFile :call MatlabRunFile()
    noremap <buffer> <Plug>(MatlabRunFile) 			:MatlabRunFile<cr>
    if !hasmapto('<Plug>(MatlabRunFile)')
        " map <buffer> <unique> <LocalLeader>r <Plug>(MatlabRunFile)
        nmap <buffer> <unique> <F5> 	<Plug>(MatlabRunFile)
    endif
endif


if exists('*MatlabRunLine')
    command! -nargs=0 MatlabRunLine :call MatlabRunLine()
    noremap <buffer> <Plug>(MatlabRunLine) 			:MatlabRunLine<cr>j
    if !hasmapto('<Plug>(MatlabRunLine)')
        " map <buffer> <unique> <LocalLeader>l <Plug>(MatlabRunLine)
        nmap <buffer> <unique> <F10> 	<Plug>(MatlabRunLine)
    endif
endif

if exists('*MatlabRunSection')
    command! -nargs=0 MatlabRunSection :call MatlabRunSection()
    noremap <buffer> <Plug>(MatlabRunSection) 			:MatlabRunSection<cr>
    if !hasmapto('<Plug>(MatlabRunSection)')
        " map <buffer> <unique> <LocalLeader>} <Plug>(MatlabRunSection)
        nmap <buffer> <unique> <C-CR> 	<Plug>(MatlabRunSection)
    endif
endif

if exists('*MatlabRunSelection')
    command! -nargs=0 MatlabRunSelection :call MatlabRunSelection()
    noremap <buffer> <Plug>(MatlabRunSelection) 		:MatlabRunSelection<CR>
    if !hasmapto('<Plug>(MatlabRunSelection)')
        " map <buffer> <unique> <LocalLeader>s <Plug>(MatlabRunSelection)
        nmap <buffer> <unique> <F9> 	<Plug>(MatlabRunSelection)
    endif
endif

if exists('*MatlabShowVariable')
    command! -nargs=0 MatlabShowVariable :call MatlabShowVariable()
    noremap <buffer> <Plug>(MatlabShowVariable) 		:MatlabShowVariable<CR>
    if !hasmapto('<Plug>(MatlabShowVariable)')
        " map <buffer> <unique> <LocalLeader>m <Plug>(MatlabShowVariable)
        nmap <buffer> <unique> <F8> 	<Plug>(MatlabShowVariable)
    endif
endif


